from django.db import models

# Create your models here.


from core.models import DbAuditModel



class MenuModel(DbAuditModel):
    """菜单模型"""
    name = models.CharField(max_length=20, verbose_name="菜单名称",help_text="菜单名称", blank=True)
    icon = models.CharField(max_length=100, verbose_name="菜单图标",help_text="菜单图标", blank=True)
    path = models.CharField(max_length=128, verbose_name="菜单url",help_text="菜单url", blank=True)
    # 设置pid可以为0，表示是根节点
    pid = models.IntegerField(default=0,verbose_name="父级菜单",help_text="父级菜单", blank=True)
    TYPE_CHOICES = (
        (0, "目录"),
        (1, "组件"),
        (2, "按钮"),
        (3, "数据")
    )
    type = models.IntegerField(
        choices=TYPE_CHOICES,
        default=0,
        verbose_name="菜单类型",
        blank=True,
        help_text="菜单类型",
    )
    STATUS_CHOICES = (
        (1, "正常"),
        (5, "激活"),
        (9, "删除"),
    )
    status = models.SmallIntegerField(
        choices=STATUS_CHOICES,
        default=1,
        verbose_name="状态",
        null=True,
        blank=True,
        help_text="状态",)
    component = models.CharField(max_length=128, help_text="组件地址", blank=True)
    identifier = models.CharField(max_length=30, help_text="权限标识 user:add", blank=True)
    api = models.CharField(max_length=128, help_text="接口地址", blank=True)
    method = models.CharField(max_length=10, help_text="接口请求方式", blank=True)
    
    class Meta:
        db_table = "system_menu"
        verbose_name = "菜单管理"
        verbose_name_plural = verbose_name
        # 排序
        ordering =("type","id",)
    
    
    def __str__(self):
        return self.name


class RoleMenuModel(DbAuditModel):
    """角色菜单模型"""
    role = models.ForeignKey("system.Role", verbose_name="角色", on_delete=models.CASCADE, related_name="menus")
    menu = models.ForeignKey(MenuModel, verbose_name="菜单", on_delete=models.CASCADE, related_name="roles")
    STATUS_CHOICES = (
        (1, "正常"),
        (5, "激活"),
        (9, "删除"),
    )
    status = models.SmallIntegerField(
        choices=STATUS_CHOICES,
        default=1,
        verbose_name="状态",
        null=True,
        blank=True,
        help_text="状态",)
    class Meta:
        db_table = "system_role_menu"
        verbose_name = "角色菜单管理"
        verbose_name_plural = verbose_name
        # 排序
        ordering = ("id",)
        # 确保每个菜单和角色的组合在数据库中是唯一的，防止重复记录。
        unique_together = ("menu", "role")
        